package system

import (
	consts2 "MoSkeleton/config/consts"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	"MoSkeleton/web/pogos/request/system"
	"MoSkeleton/web/v1/http/base"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
	"time"
)

type SysConfigCtrl struct {
	base.BaseCtrl
}

func (s *SysConfigCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.Tkey_TitleSysConfigMsg)
	return msg
}

// AddConfig 添加配置
// @Tags 参数管理
// @Summary 添加配置
// @Security ApiKeyAuth
// @Param data body system.SysConfigAddReq true "添加配置"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Router /system/config [post]
func (sc *SysConfigCtrl) AddConfig(c *gin.Context) {
	configAddReq := system.SysConfigAddReq{}
	if err := c.ShouldBindJSON(&configAddReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	configAddReq.CreatedBy = utils.GetUsernameFromGin(c)
	configAddReq.UpdatedBy = configAddReq.CreatedBy
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysConfigServiceInst.AddConfig(&configAddReq); err != nil {
		global.MoLogger.Info("添加配置失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_ConfigAddFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_CommCreatFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommCreatSuccessMsg, nil)
}

// DeleteById 根据id删除配置
// @Tags 参数管理
// @Summary 根据id删除配置
// @Security ApiKeyAuth
// @Param id path int true "配置id"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Router /system/config/{id} [delete]
func (sc *SysConfigCtrl) DeleteById(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysConfigServiceInst.DeleteById(id); err != nil {
		global.MoLogger.Info("删除配置失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_ConfigDelFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_CommDeleteFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommDeleteSuccessMsg, nil)
}

// RefreshCache 刷新配置缓存
// @Tags 参数管理
// @security ApiKeyAuth
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Router /system/config/refreshCache [delete]
func (sc *SysConfigCtrl) RefreshCache(c *gin.Context) {
	if err := services.ServiceGroupInst.SystemGroupInst.SysConfigServiceInst.RefreshCache(); err != nil {
		global.MoLogger.Info("刷新配置缓存失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_ConfigRefreshFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommRefreshSuccessMsg, nil)
}

// GetConfigPage 分页查询配置
// @Tags 参数管理
// @Summary 分页查询配置
// @Security ApiKeyAuth
// @Param data query system.SysConfigPageReq true "分页查询配置"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Router /system/config/list [get]
func (sc *SysConfigCtrl) GetConfigPage(c *gin.Context) {
	configPageReq := system.SysConfigPageReq{}
	if err := c.ShouldBind(&configPageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	configPageReq.ShouldBindParams(c)
	if pageResult, err := services.ServiceGroupInst.SystemGroupInst.SysConfigServiceInst.GetConfigPage(&configPageReq); err != nil {
		global.MoLogger.Info("查询配置失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_ConfigGetFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, pageResult)
		return
	}
}

// ExportConfig 导出配置
// @Tags 参数管理
// @Param data body system.SysConfigPageReq true "导出配置"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Router /system/config/export [POST]
func (sc *SysConfigCtrl) ExportConfig(c *gin.Context) {
	sc.IgnoreLogResponseData(c)
	configReq := system.SysConfigPageReq{}
	if err := c.ShouldBind(&configReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if buf, err := services.ServiceGroupInst.SystemGroupInst.SysConfigServiceInst.ExportConfig(&configReq); err != nil {
		global.MoLogger.Info("导出配置失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_ConfigExportFailMsg)
		return
	} else {
		response.SuccessWithFileData(c, fmt.Sprintf(consts2.Excel_Export_Config, time.Now().Unix()), buf)
	}
}

// UpdateConfigById 根据id更新配置
// @Tags 参数管理
// @Summary 根据id更新配置
// @Security ApiKeyAuth
// @Param data body system.SysConfigUpdateReq true "根据id更新配置"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Router /system/config [put]
func (sc *SysConfigCtrl) UpdateConfigById(c *gin.Context) {
	configUpdateReq := system.SysConfigUpdateReq{}
	if err := c.ShouldBindJSON(&configUpdateReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	configUpdateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysConfigServiceInst.UpdateConfigById(&configUpdateReq); err != nil {
		global.MoLogger.Info("更新配置失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_ConfigUpdateFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_CommUpdateFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
}

// GetConfigById 根据id查询配置
// @Tags 参数管理
// @Summary 根据id查询配置
// @Security ApiKeyAuth
// @Param id path int true "配置id"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Router /system/config/{id} [get]
func (sc *SysConfigCtrl) GetConfigById(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if configItemResp, err := services.ServiceGroupInst.SystemGroupInst.SysConfigServiceInst.GetConfigById(id); err != nil {
		global.MoLogger.Info("查询配置失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_ConfigGetFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, configItemResp)
		return
	}
}

// GetByConfigKey 根据key查询配置
// @Tags 参数管理
// @Summary 根据key查询配置
// @Security ApiKeyAuth
// @Param configKey path string true "配置key"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "msg": ""}"
// @Router /system/config/key/{configKey} [get]
func (sc *SysConfigCtrl) GetByConfigKey(c *gin.Context) {
	configKey := c.Param("configKey")
	if configItemResp, err := services.ServiceGroupInst.SystemGroupInst.SysConfigServiceInst.GetByConfigKey(configKey); err != nil {
		global.MoLogger.Info("查询配置失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_ConfigGetFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommonGetListSuccessMsg, configItemResp.ConfigValue)
		return
	}
}
